package com.holyn.guofang.view.my;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.wo.AttachedCardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CouponInfoWo;
import com.holyn.guofang.utils.HtmlUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Holyn on 2017/8/28.
 */

public class MyRechargeAdapter2 extends BaseSectionQuickAdapter<MyRechargeSection, BaseViewHolder>{
    private View footerAttache;
    private TextView tvGetAttache;
    private OnGetAttacheClickListener onGetAttacheClickListener;
    private RelativeLayout rlAttachedCardAll;
    private TextView tvAttachedCardName;
    private TextView tvPrice;
    private EditText etAttachedCount;
    private AttachedCardDictionaryWo.DataBean attachedCard = null;

    private View footerCoupon;
    private TextView tvGetCoupon;
    private OnGetCouponClickListener onGetCouponClickListener;
    private LinearLayout llCouponCardAll;
    private TextView tvCouponCardName;
    private TextView tvRate;
    private CouponInfoWo.DataBean couponCard = null;


    public MyRechargeAdapter2(Context context, List<MyRechargeSection> data) {
        super(R.layout.item_my_recharge, R.layout.item_my_recharge_section, data);
        initFooterAttacheView(context);
        initFooterCouponView(context);

        addFooterView(footerAttache);
        addFooterView(footerCoupon);
    }

    private void initFooterAttacheView(Context context){
        footerAttache = LayoutInflater.from(context).inflate(R.layout.footer_my_recharge_attache_card, null);
        tvGetAttache = (TextView) footerAttache.findViewById(R.id.tv_get_attache);
        tvGetAttache.setText(HtmlUtil.fromHtml("<u>"+"获取附属卡"+"</u>"));
        tvGetAttache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardDictionaryWo.DataBean curCheckMainCard = getCheckedMainCard();
                Integer parentID = 0;
                if (curCheckMainCard != null){
                    parentID = curCheckMainCard.getId();
                }
                onGetAttacheClickListener.onGetAttacheClick(parentID);
            }
        });
        tvGetAttache.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                clearCouponInfoData();
            }
        });
        rlAttachedCardAll = (RelativeLayout) footerAttache.findViewById(R.id.rl_card_all);
        tvAttachedCardName = (TextView) footerAttache.findViewById(R.id.tv_card_name);
        tvPrice = (TextView) footerAttache.findViewById(R.id.tv_price);
        etAttachedCount = (EditText) footerAttache.findViewById(R.id.et_count);
    }

    private void initFooterCouponView(Context context){
        footerCoupon = LayoutInflater.from(context).inflate(R.layout.footer_my_recharge_coupon_info, null);
        tvGetCoupon = (TextView) footerCoupon.findViewById(R.id.tv_get_coupon);
        tvGetCoupon.setText(HtmlUtil.fromHtml("<u>"+"申请优惠"+"</u>"));
        tvGetCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer type = -1;//购卡类型(1:购买主卡，2:只购买附属卡)
                Integer cardCount = 0;
                CardDictionaryWo.DataBean curCheckMainCard = getCheckedMainCard();
                if (curCheckMainCard != null){//勾选了主卡
                    type = 1;
                } else {//没有勾选了主卡
                    String cardCountStr = etAttachedCount.getText().toString().trim();
                    try {
                        cardCount = Integer.parseInt(cardCountStr);
                    } catch (Exception e){
                        Toast.makeText(context, "请输入正确的附属卡数量", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (cardCount > 0){
                        type = 2;
                    } else {
                        Toast.makeText(context, "附属卡数量必须大于0", Toast.LENGTH_SHORT).show();
                    }
                }

                if (type == -1){
                    Toast.makeText(context, "请勾选主卡或输入附属卡数量", Toast.LENGTH_SHORT).show();
                    return;
                }

                onGetCouponClickListener.onGetCouponClick(type, cardCount);
            }
        });
        llCouponCardAll = (LinearLayout) footerCoupon.findViewById(R.id.ll_card_all);
        tvCouponCardName = (TextView) footerAttache.findViewById(R.id.tv_card_name);
        tvRate = (TextView) footerCoupon.findViewById(R.id.tv_rate);
    }

    @Override
    protected void convertHead(BaseViewHolder baseViewHolder, MyRechargeSection myRechargeSection) {
        baseViewHolder.setText(R.id.tv_title_name, myRechargeSection.header);
        if (myRechargeSection.isShowDivide()){
            baseViewHolder.setVisible(R.id.ll_divide, true);
        } else {
            baseViewHolder.setVisible(R.id.ll_divide, false);
        }
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, MyRechargeSection myRechargeSection) {
        boolean isCheck = myRechargeSection.isCheck();
        ImageView ivCard = baseViewHolder.getView(R.id.iv_card);
        if (isCheck){
            ivCard.setImageResource(R.drawable.ic_card1_pre);
        } else {
            ivCard.setImageResource(R.drawable.ic_card1_nor);
        }
        ivCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (MyRechargeSection section : getData()){
                    section.setCheck(false);
                }
                getData().get(baseViewHolder.getAdapterPosition()).setCheck(!isCheck);
                clearAttachedCardData();
                clearCouponInfoData();
                notifyDataSetChanged();
            }
        });

        baseViewHolder.setText(R.id.tv_card_name, "国方卡\n("+myRechargeSection.t.getName()+")");
        baseViewHolder.setText(R.id.tv_price, "¥"+myRechargeSection.t.getPrice()+"元");
    }

    public boolean isSelectCard(){
        Integer type = -1;//购卡类型(1:购买主卡，2:只购买附属卡)
        Integer cardCount = 0;
        CardDictionaryWo.DataBean curCheckMainCard = getCheckedMainCard();
        if (curCheckMainCard != null){//勾选了主卡
            type = 1;
        } else {//没有勾选了主卡
            String cardCountStr = etAttachedCount.getText().toString().trim();
            try {
                cardCount = Integer.parseInt(cardCountStr);
            } catch (Exception e){

            }
            if (cardCount > 0){
                type = 2;
            }
        }
        if (type == -1){
            return false;
        } else {
            return true;
        }
    }

    public Integer getCardID(){
        CardDictionaryWo.DataBean curCheckMainCard = getCheckedMainCard();
        Integer parentID = 0;
        if (curCheckMainCard != null){
            parentID = curCheckMainCard.getId();
        }
        return parentID;
    }

    public Integer getAttenedCardID(){
        if (attachedCard == null){
            return 0;
        }
        Integer cardCount = 0;
        String cardCountStr = etAttachedCount.getText().toString().trim();
        try {
            cardCount = Integer.parseInt(cardCountStr);
        } catch (Exception e){

        }
        if (cardCount > 0){
            return attachedCard.getId();
        }

        return 0;
    }

    public Integer getAttenedCardCount(){
        Integer cardCount = 0;
        String cardCountStr = etAttachedCount.getText().toString().trim();
        try {
            cardCount = Integer.parseInt(cardCountStr);
        } catch (Exception e){

        }
        return cardCount;
    }

    public Integer getNeedCoupon(){
        if (couponCard == null){
            return 0;
        } else {
            return 1;
        }
    }

    public void setOnGetAttacheClickListener(OnGetAttacheClickListener onGetAttacheClickListener) {
        this.onGetAttacheClickListener = onGetAttacheClickListener;
    }

    public void setOnGetCouponClickListener(OnGetCouponClickListener onGetCouponClickListener) {
        this.onGetCouponClickListener = onGetCouponClickListener;
    }

    public void addAttachedCardData(AttachedCardDictionaryWo attachedCardDictionaryWo){//添加附属卡
        List<AttachedCardDictionaryWo.DataBean> dataList = attachedCardDictionaryWo.getData();
        if(dataList != null){
            if (dataList.size() > 0){
                rlAttachedCardAll.setVisibility(View.VISIBLE);
                attachedCard = dataList.get(0);
                tvAttachedCardName.setText("国方卡\n("+attachedCard.getName()+")");
                tvPrice.setText("¥"+attachedCard.getPrice()+"元");
            }
        }
    }

    public void clearAttachedCardData(){
        rlAttachedCardAll.setVisibility(View.GONE);
        attachedCard = null;
        tvPrice.setText("");
    }

    public void addCouponInfoData(CouponInfoWo couponInfoWo){//添加优惠卡
        List<CouponInfoWo.DataBean> dataList = couponInfoWo.getData();
        if(dataList != null){
            if (dataList.size() > 0){
                llCouponCardAll.setVisibility(View.VISIBLE);
                couponCard = dataList.get(0);
                tvCouponCardName.setText(attachedCard.getName());
                tvRate.setText("折扣"+couponCard.getDiscountRate()+"%");
            }
        }
    }

    public void clearCouponInfoData(){
        llCouponCardAll.setVisibility(View.GONE);
        couponCard = null;
        tvRate.setText("");
    }

    public CardDictionaryWo.DataBean getCheckedMainCard(){
        for (MyRechargeSection myRechargeSection : getData()){
            if (myRechargeSection.isCheck()){
                return myRechargeSection.t;
            }
        }
        return null;
    }

    public interface OnGetAttacheClickListener{
        void onGetAttacheClick(Integer parentID);
    }

    public interface OnGetCouponClickListener{
        void onGetCouponClick(Integer type, Integer cardCount);
    }
}
